﻿using Pay.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Pay.IO
{
    /// <summary>
    /// 文件操作
    /// </summary>
    public class LotFile
    {
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ReadAllText(string path)
        {
            CreateFile(path);
            if (Directory.Exists(Path.GetDirectoryName(path)))
            {
                return File.ReadAllText(path);
            }
            return string.Empty;
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <typeparam name="T">读取后转换的类型</typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T ReadAllText<T>(string path) where T : class
        {
            CreateFile(path);
            if (Directory.Exists(Path.GetDirectoryName(path)))
            {
                var data = ReadAllText(path);
                if (data == null || data == "")
                {
                    return null;
                }

                return JsonHelper.DeserializeObject<T>(data);
            }
            return null;
        }

        /// <summary>
        /// 追加内容到文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="contents"></param>
        public static void AppendAllText(string path, string contents)
        {
            CreateFile(path);
            File.AppendAllText(path, contents);

        }

        /// <summary>
        /// 覆盖文件所有内容
        /// </summary>
        /// <param name="path"></param>
        /// <param name="contents"></param>
        public static void WriteAllText(string path, string contents)
        {
            CreateFile(path);
            File.WriteAllText(path, contents);

        }

        /// <summary>
        /// 覆盖文件所有内容
        /// </summary>
        /// <param name="path"></param>
        /// <param name="contents"></param>
        public static void WriteAllText(string path, object contents)
        {
            CreateFile(path);
            var data = JsonHelper.SerializeObject(contents);
            WriteAllText(path, data);
        }

        private static void CreateDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        public static void CreateFile(string path)
        {
            CreateDirectory(Path.GetDirectoryName(path));
            if (!File.Exists(path))
            {
                using (var f = File.Create(path))
                {

                }
            }
        }

        public static string GetFileMD5(string filename)
        {
            try
            {
                FileStream file = new FileStream(filename, FileMode.Open);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] retval = md5.ComputeHash(file);
                file.Close();

                StringBuilder sc = new StringBuilder();
                for (int i = 0; i < retval.Length; i++)
                {
                    sc.Append(retval[i].ToString("x2"));
                }
                return sc.ToString();
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}
